package com.yuyou.train.user.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;  import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.train.service.api.user.entity.User;
import com.yuyou.train.service.api.user.entity.UserPraise;
import com.yuyou.train.user.service.IUserPraiseService;
import com.yuyou.train.user.service.IUserService;
import com.yuyou.train.user.vo.req.ReqUserPraise;
import com.yuyou.train.user.vo.resp.RespUserRaiseTop;
import com.yuyou.train.util.bean.LoginRedisObj;
import com.yuyou.train.util.bean.ReturnResult;
import com.yuyou.train.util.enums.user.UserPraiseEnums;
import com.yuyou.train.util.tool.DateUtil;
import com.yuyou.train.util.tool.DefindBeanUtils;
import com.yuyou.train.util.tool.Sequence;


@Validated
@RestController
@RequestMapping(value="/api/userPraise")
public class UserPraiseController extends BaseController{

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private IUserPraiseService userPraiseService;

	@Autowired
	private IUserService userService;

	@ApiOperation(value = "点赞(前台)")
	@RequestMapping(value="/addPraise", method = RequestMethod.POST)
	public ReturnResult<Void> addPraise(@Validated @ModelAttribute ReqUserPraise reqUserPraise, HttpServletRequest request){
		LoginRedisObj loginObj = this.getUserObj(request);
		UserPraise userPraise = new UserPraise();
		DefindBeanUtils.copyProperties(reqUserPraise, userPraise);
		UserPraise userPraiseQuery = userPraiseService.findPraiseByForeignIdAndUserId(userPraise.getForeignId(), loginObj.getUserId());
		// 点过赞的不会再插入
		if(null == userPraiseQuery){
			userPraise.setId(Sequence.getSequence().getNextId());
			userPraise.setUserId(loginObj.getUserId());
			userPraise.setPoints(1); // 点赞一次
			userPraise.setCreateTime(DateUtil.now());
			userPraiseService.insert(userPraise);
		}
		return ReturnResult.success("OK");
	}

	@ApiOperation(value = "取消点赞(前台)")
	@RequestMapping(value="/delPraise", method = RequestMethod.POST)
	public ReturnResult<Void> delPraise(@ApiParam(value = "取消点赞对象 id",required = true) @RequestParam(value = "foreignId") String foreignId,
								  		HttpServletRequest request){
		LoginRedisObj loginObj = this.getUserObj(request);
		userPraiseService.deleteByfForeignIdUserId(foreignId, loginObj.getUserId());
		return ReturnResult.success("OK");
	}

	@ApiOperation(value = "最多赞推荐(前台)")
	@RequestMapping(value="/findUserPraiseTop", method = RequestMethod.POST)
	public ReturnResult<List<RespUserRaiseTop>> findUserPraiseTop(HttpServletRequest request){

		LoginRedisObj userObj = this.getUserObj(request);
		User user = userService.selectByPrimaryKey(userObj.getUserId());
		List<RespUserRaiseTop> praiseTopList = userPraiseService.findHwPraiseTop(user.getClassId(), UserPraiseEnums.isPraise.yes.getValue());
		return ReturnResult.success(praiseTopList);
	}


}
